﻿using LdtDataEngin.ModuleDetail.CaseCauseElementOpt;
using LdtDataEngin.ModuleDetail.CommonModule.XmlCodeCommon;
using LdtDataEngin.Moudle;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.Collection;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace LdtDataEngin.Moudle
{
    /// <summary>
    /// 案由优选模块
    /// </summary>
    public class MoudleCaseCauseOptimize : MoudleBase
    {
        private IMoudle _moudle;
        private static string _moudleResult;
        public MoudleCaseCauseOptimize(IMoudle moudle)
        {
            _moudle = moudle;
        }
        public override void MoudleHandle(DataContext dataContext)
        {
            //执行当前模块的相关业务逻辑；
            string moudleName = typeof(MoudleCaseCauseOptimize).FullName;
            moudleName += "_" + this.GetHashCode();
            // 设置模块进度
            dataContext.InteractiveShow.ModuleProcessShow?.Invoke(moudleName, 0, dataContext.InteractiveShow.inputParameterModel);
            // 创建计时器
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            try
            {
                dataContext.InteractiveShow.LogShow("***************************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--->开始执行案由要素优选模块*****************************");
                XmlCodeStartModel xmlCodeStartModel = new XmlCodeStartModel() { dataContext = dataContext };
                var configModel = dataContext.DataLayerSys.GetConfigModel(moudleName);
                if (xmlCodeStartModel.Extract(configModel))
                {
                    CaseCauseOptimization caseCauseOptimization = new CaseCauseOptimization(dataContext, moudleName);
                    caseCauseOptimization.Optimization();
                }

            }
            catch (Exception ex)
            {
                dataContext.InteractiveShow.LogShow("案由要素优选模块错误：" + ex.ToString());
            }
            dataContext.InteractiveShow.LogShow("***************************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--->案由要素优选模块完成*****************************");
            // 设置模块进度
            stopwatch.Stop();
            _moudleResult = dataContext.DataLayerOpt.OptimizationXml.OuterXml;
            dataContext.InteractiveShow.ModuleProcessShow?.Invoke(moudleName + "_" + stopwatch.Elapsed.TotalMilliseconds.ToString("0"), 100, dataContext.InteractiveShow.inputParameterModel);
            //执行完毕调用下一个模块；
            _moudle.MoudleHandle(dataContext);
        }

        /// <summary>
        /// 模块处理结果
        /// </summary>
        /// <returns></returns>
        public static HandleResultModel MoudleHandleResult()
        {
            HandleResultModel resultModel = new HandleResultModel();
            if (!string.IsNullOrWhiteSpace(_moudleResult))
            {

                resultModel.ResultType = "1";
                resultModel.ResultData = _moudleResult;
            }
            return resultModel;
        }
    }
}
